home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xarchie-2.0.9 / stcopy.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  2KB  |  103 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  *
  7.  * v1.2.0 - 09/17/91 (bpk) - added BULL & USG stuff, thanks to Jim Sillas
  8.  */
  9.  
  10. #include <copyright.h>
  11. #include <stdio.h>
  12.  
  13. #include "config.h"                /* gf */
  14. #include "stringdefs.h"                /* gf */
  15.  
  16. #if defined(MSDOS)
  17. # include <stdlib.h>
  18. #endif
  19.  
  20. char    *stcopyr();
  21.  
  22. int    string_count = 0;
  23. int    string_max = 0;
  24.  
  25. /*
  26.  * stcopy - allocate space for and copy a string
  27.  *
  28.  *     STCOPY takes a string as an argument, allocates space for
  29.  *     a copy of the string, copies the string to the allocated space,
  30.  *     and returns a pointer to the copy.
  31.  */
  32.  
  33. char *
  34. stcopy(st)
  35.     char    *st;
  36.     {
  37.       if (!st) return(NULL);
  38.       if (string_max < ++string_count) string_max = string_count;
  39.  
  40.       return strcpy((char *)malloc(strlen(st) + 1), st);
  41.     }
  42.  
  43. /*
  44.  * stcopyr - copy a string allocating space if necessary
  45.  *
  46.  *     STCOPYR takes a string, S, as an argument, and a pointer to a second
  47.  *     string, R, which is to be replaced by S.  If R is long enough to
  48.  *     hold S, S is copied.  Otherwise, new space is allocated, and R is
  49.  *     freed.  S is then copied to the newly allocated space.  If S is
  50.  *     NULL, then R is freed and NULL is returned.
  51.  *
  52.  *     In any event, STCOPYR returns a pointer to the new copy of S,
  53.  *     or a NULL pointer.
  54.  */
  55. char *
  56. stcopyr(s,r)
  57.     char    *s;
  58.     char    *r;
  59.     {
  60.     int    sl;
  61.  
  62.     if(!s && r) {
  63.         free(r);
  64.         string_count--;
  65.         return(NULL);
  66.     }
  67.     else if (!s) return(NULL);
  68.  
  69.     sl = strlen(s) + 1;
  70.  
  71.     if(r) {
  72.         if ((strlen(r) + 1) < sl) {
  73.         free(r);
  74.         r = (char *) malloc(sl);
  75.         }
  76.     }
  77.     else {
  78.         r = (char *) malloc(sl);
  79.         string_count++;
  80.         if(string_max < string_count) string_max = string_count;
  81.     }
  82.         
  83.     return strcpy(r,s);
  84.     }
  85.  
  86. /*
  87.  * stfree - free space allocated by stcopy or stalloc
  88.  *
  89.  *     STFREE takes a string that was returned by stcopy or stalloc 
  90.  *     and frees the space that was allocated for the string.
  91.  */
  92. void
  93. stfree(st)
  94.     char *st;
  95.     {
  96.     if(st) {
  97.         free(st);
  98.         string_count--;
  99.     }
  100.     }
  101.  
  102.  
  103.